<template>
	<view class="x-location">定位
		<!-- 		   <view >
		    <text>当前位置：</text>    
		    <text >[{{latitude}}] [{{longitude}}]</text>
		   </view>
		   <view >
		    <text>所在城市：</text>
		    <text >{{province}} {{city}}</text>
		  </view> -->
	</view>
</template>


<script>
	const app = getApp();
	var QQMapWX = require('@/utils/qqmap-wx-jssdk1.0/qqmap-wx-jssdk.min.js'); //引入SDK文件
	var qqmapsdk;
	export default {
		onLoad: function() {
		},

		data() {
			return {
				province: '',
				city: '',
				latitude: '',
				longitude: ''
			}
		},
		created() {
			qqmapsdk = new QQMapWX({
				key: 'CR7BZ-YCEL3-3II3U-OQEMG-3LQPK-NIFJG' //换成你自己申请的key秘钥
				// key: 'EQSBZ-JGC6N-NVTF5-SJSTC-UQD57-ZXFMB'
			});
			console.log("qqmapsdk=", qqmapsdk)
		},
		
		methods: {
			getUserLocation: function() {
				return new Promise((resolve, reject) => {
					// 广东省,深圳市,龙岗区
					let vm = this;
					wx.getSetting({
						success: async (res) => {
							// 
							if (res.authSetting['scope.userLocation'] != undefined && res
								.authSetting['scope.userLocation'] != true) { //如果没有授权就提示需要授权
								
								wx.showModal({
									title: '请求授权当前位置',
									content: '需要获取您的地理位置，请确认授权',
									success: async (res) => {
										if (res.cancel) {
											wx.showToast({
												title: '拒绝授权',
												icon: 'none',
												duration: 1000
											})
											reject("用户拒绝授权")
										} else if (res.confirm) {
											wx.openSetting({
												success: async (dataAu) => {
													if (dataAu.authSetting[
															"scope.userLocation"
														] == true) {

														wx.showToast({
															title: '授权成功',
															icon: 'success',
															duration: 1000
														})
														//再次授权，调用wx.getLocation的API
														let {
															latitude,
															longitude
														} = await vm
															.getLocation()

														let {
															province,
															city,
															district,
															detail
														} = await vm.getLocal(latitude,longitude)
														
														console.log("province=", province, ",city=", city, ",district=", district, ",detail",detail)
														resolve({
															province,
															city,
															district,
															detail
														})
														// vm.getLocation()
													} else {
														wx.showToast({
															title: '授权失败',
															icon: 'none',
															duration: 1000
														})
														reject("授权失败")
													}
												}
											})
										}
									}
								})
							} else if (res.authSetting['scope.userLocation'] == undefined) {

								//调用wx.getLocation的API
								// vm.getLocation();
								let {
									latitude,
									longitude
								} = await vm.getLocation()

								let {
									province,
									city,
									district,
									detail
								} = await vm.getLocal(latitude, longitude)
								console.log("111 province=", province, ",city=", city,
									",district=", district, ",detail=", detail)
								resolve({
									province,
									city,
									district,
									detail
								})
							} else {
								//调用wx.getLocation的API
								let {
									latitude,
									longitude
								} = await vm.getLocation()

								let {
									province,
									city,
									district,
									detail
								} = await vm.getLocal(latitude, longitude)
								console.log("222 province=", province, ",city=", city,
									",district=", district,",detail=", detail)
								resolve({
									province,
									city,
									district,
									detail
								})
							}
						}
					})
				}).catch((error) => {
					reject(error)
				})

			},
			// 微信获得经纬度
			getLocation: function() {
				return new Promise((resolve, reject) => {
					let vm = this;
					wx.getLocation({
						type: 'wgs84',
						success: (res) => {
							console.log(JSON.stringify(res))
							var latitude = res.latitude
							var longitude = res.longitude
							var speed = res.speed
							var accuracy = res.accuracy;
							resolve({
								latitude,
								longitude
							})
						},
						fail: (res) => {
							console.log('fail' + JSON.stringify(res))
							reject(JSON.stringify(res))
						}
					})
				})

			},
			// 获取当前地理位置
			getLocal: function(latitude, longitude) {
				return new Promise((resolve, reject) => {
					let vm = this;
					qqmapsdk.reverseGeocoder({
						location: {
							latitude: latitude,
							longitude: longitude
						},
						success: (res) => {
							console.log("success=", JSON.stringify(res));
							let province = res.result.ad_info.province
							let city = res.result.ad_info.city
							let district = res.result.ad_info.district
							let detail = res.result.formatted_addresses.recommend //详细地址
							resolve({
								province,
								city,
								district,
								detail //详细地址
							})
							// "formatted_addresses": {
							//     "recommend": "深圳市龙岗区人民政府(龙翔大道北)",
							//     "rough": "深圳市龙岗区人民政府(龙翔大道北)",
							//     "standard_address": "广东省深圳市龙岗区龙翔大道8033号"
							// },
							// "ad_info": {
							//     "nation_code": "156",
							//     "adcode": "440307",
							//     "phone_area_code": "0755",
							//     "city_code": "156440300",
							//     "name": "中国,广东省,深圳市,龙岗区",
							//     "location": {
							//         "lat": 22.720889,
							//         "lng": 114.246884
							//     },
							//     "nation": "中国",
							//     "province": "广东省",
							//     "city": "深圳市",
							//     "district": "龙岗区"
							// },
						},
						fail: (res) => {
							console.log("fail=", res);
							reject(res)
						},
						complete: (res) => {
							console.log("complete=", res);
						}
					});
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.x-location {
		color: #fc4e27;
	}
</style>